const removeKeepAliveCacheForVueInstance = vueInstance => {
  let key =
    vueInstance.$vnode.key ||
    vueInstance.$vnode.componentOptions.Ctor.cid +
      (vueInstance.$vnode.componentOptions.tag
        ? `::${vueInstance.$vnode.componentOptions.tag}`
        : '')
  let cache = vueInstance.$vnode.parent.componentInstance.cache
  let keys = vueInstance.$vnode.parent.componentInstance.keys
  if (cache[key]) {
    vueInstance.$destroy()
    delete cache[key]
    let index = keys.indexOf(key)
    if (index > -1) {
      keys.splice(index, 1)
    }
  }
}

export default {
  beforeRouteLeave(to, from, next) {
    if (to.name === 'settingPage') {
      removeKeepAliveCacheForVueInstance(this)
      from.meta.keepAlive = false
      next()
    } else {
      from.meta.keepAlive = true
      next()
    }
  },
  beforeRouteEnter(to, from, next) {
    to.meta.keepAlive = true
    next()
  }
}
